package ans.p146;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * #      146
 * 名称   LRU 缓存机制
 * 链接   https://leetcode-cn.com/problems/lru-cache
 * 题解   [993](https://leetcode-cn.com/problems/lru-cache/solution)
 * 通过率 52.1%
 * 难道   中等
 */
//todo LRU算法尝试手写
class LRUCache extends LinkedHashMap<Integer, Integer> {
    private int capacity;

    public LRUCache(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}


